package top.lilele.adminSystem.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lilele.adminSystem.dao.MeetMinutesDao;
import top.lilele.adminSystem.entity.AdminBusinessRelationFileEntity;
import top.lilele.adminSystem.entity.AdminMeetMinutesEntity;
import top.lilele.adminSystem.mapper.AdminBusinessRelationFileMapper;
import top.lilele.adminSystem.mapper.AdminMeetMinutesMapper;
import top.lilele.adminSystem.numEnum.BusinessRelationFileEnum;
import top.lilele.adminSystem.numEnum.LogEnum;
import top.lilele.adminSystem.utils.Utils;
import top.lilele.adminSystem.vo.MeetMinutesVo;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lilele
 */
@Service
@Slf4j
public class AdminMeetMinutesService {
    @Resource
    AdminLogService adminLogService;

    @Resource
    AdminMeetMinutesMapper adminMeetMinutesMapper;

    @Resource
    AdminFileService adminFileService;

    @Resource
    AdminBusinessRelationFileMapper adminBusinessRelationFileMapper;

    @Transactional(rollbackFor = Exception.class)
    public void add(MeetMinutesDao meetMinutesDao) {
        adminMeetMinutesMapper.insert(meetMinutesDao);
        for (String files : meetMinutesDao.getFileIds()) {
            AdminBusinessRelationFileEntity adminBusinessRelationFileEntity = new AdminBusinessRelationFileEntity();
            adminBusinessRelationFileEntity.setBusinessId(meetMinutesDao.getId());
            adminBusinessRelationFileEntity.setType(BusinessRelationFileEnum.MEET_MINUTES.getType());
            adminBusinessRelationFileEntity.setFileId(files);
            adminBusinessRelationFileMapper.insert(adminBusinessRelationFileEntity);
        }
        adminLogService.add(StpUtil.getLoginIdAsString(), "创建了" + meetMinutesDao.getTitle(), LogEnum.MEET_MINUTES.getType(), meetMinutesDao.getId());
    }

    public List<MeetMinutesVo> queryMeetMinutesList(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("amm.projectId", id);
        List<MeetMinutesVo> list = adminMeetMinutesMapper.queryMeetMinutesList(queryWrapper);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateMeetMinutesById(MeetMinutesDao meetMinutesDao) {
        AdminMeetMinutesEntity adminMeetMinutesEntity = new AdminMeetMinutesEntity();
        BeanUtils.copyProperties(meetMinutesDao, adminMeetMinutesEntity);
        adminMeetMinutesMapper.updateById(adminMeetMinutesEntity);
    }

    /**
     * 根据id删除会议纪要
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMeetMinutesById(String id) {
        // 项目会议纪要信息
        adminMeetMinutesMapper.deleteById(id);
        this.deleteFiles(id);
        // 删除会议纪要关联文件信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Utils.getName(AdminBusinessRelationFileEntity::getBusinessId), id);
        adminBusinessRelationFileMapper.delete(queryWrapper);
    }

    @Async("asyncTaskExecutor")
    public void deleteFiles(String id) {
        // 获取项目纪要关联的文件信息
        LambdaQueryWrapper<AdminBusinessRelationFileEntity> wrapper = new LambdaQueryWrapper<AdminBusinessRelationFileEntity>().eq(AdminBusinessRelationFileEntity::getBusinessId, id);
        List<AdminBusinessRelationFileEntity> list = adminBusinessRelationFileMapper.selectList(wrapper);
        // 删除文件信息与minio的文件
        list.stream().forEach(item -> {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(Utils.getName(AdminBusinessRelationFileEntity::getFileId), item.getFileId());
            queryWrapper.ne(Utils.getName(AdminBusinessRelationFileEntity::getId), item.getId());
            List<AdminBusinessRelationFileEntity> list1 = adminBusinessRelationFileMapper.selectList(queryWrapper);
//            Integer number = allFileList.stream().filter(item0 -> {
//                return item.getFileId() == item0.getFileId() && item.getId() != item0.getId();
//            }).collect(Collectors.toList()).size();
//            log.info("业务list {}",number);
            //  文件没有被引用
            if (list1.size() == 0) {
                adminFileService.deleteFileById(item.getFileId());
            }
        });
    }
}
